package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.entity.*;
import org.jeecg.modules.mapper.*;
import org.jeecg.modules.service.IRunnerMatchService;
import org.jeecg.modules.utill.MqttUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 赛事
 * @Author: jeecg-boot
 * @Date:   2022-12-04
 * @Version: V1.0
 */
@Service
public class RunnerMatchServiceImpl extends ServiceImpl<RunnerMatchMapper, RunnerMatch> implements IRunnerMatchService {

	@Autowired
	private RunnerMatchMapper runnerMatchMapper;
	@Autowired
	private RunnerMatchPlayerMapper runnerMatchPlayerMapper;
	@Autowired
	private RunnerMatchDeviceMapper runnerMatchDeviceMapper;
	@Autowired
	private RunnerMatchGunsMapper runnerMatchGunsMapper;
	@Autowired
	private RunnerMatchInfoMapper runnerMatchInfoMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(RunnerMatch runnerMatch, List<RunnerMatchPlayer> runnerMatchPlayerList, List<RunnerMatchDevice> runnerMatchDeviceList, List<RunnerMatchGuns> runnerMatchGunsList, List<RunnerMatchInfo> runnerMatchInfoList) {
		runnerMatchMapper.insert(runnerMatch);
		if(runnerMatchPlayerList!=null && runnerMatchPlayerList.size()>0) {
			for(RunnerMatchPlayer entity:runnerMatchPlayerList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchPlayerMapper.insert(entity);
			}
		}
		if(runnerMatchDeviceList!=null && runnerMatchDeviceList.size()>0) {
			for(RunnerMatchDevice entity:runnerMatchDeviceList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchDeviceMapper.insert(entity);
			}
		}
		if(runnerMatchGunsList!=null && runnerMatchGunsList.size()>0) {
			for(RunnerMatchGuns entity:runnerMatchGunsList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchGunsMapper.insert(entity);
			}
		}
		if(runnerMatchInfoList!=null && runnerMatchInfoList.size()>0) {
			for(RunnerMatchInfo entity:runnerMatchInfoList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchInfoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(RunnerMatch runnerMatch,List<RunnerMatchPlayer> runnerMatchPlayerList,List<RunnerMatchDevice> runnerMatchDeviceList,List<RunnerMatchGuns> runnerMatchGunsList,List<RunnerMatchInfo> runnerMatchInfoList) {
		runnerMatchMapper.updateById(runnerMatch);
		
		//1.先删除子表数据
		runnerMatchPlayerMapper.deleteByMainId(runnerMatch.getId());
		runnerMatchDeviceMapper.deleteByMainId(runnerMatch.getId());
		runnerMatchGunsMapper.deleteByMainId(runnerMatch.getId());
		runnerMatchInfoMapper.deleteByMainId(runnerMatch.getId());
		
		//2.子表数据重新插入
		if(runnerMatchPlayerList!=null && runnerMatchPlayerList.size()>0) {
			for(RunnerMatchPlayer entity:runnerMatchPlayerList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchPlayerMapper.insert(entity);
			}
		}
		if(runnerMatchDeviceList!=null && runnerMatchDeviceList.size()>0) {
			for(RunnerMatchDevice entity:runnerMatchDeviceList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchDeviceMapper.insert(entity);
			}
		}
		if(runnerMatchGunsList!=null && runnerMatchGunsList.size()>0) {
			for(RunnerMatchGuns entity:runnerMatchGunsList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchGunsMapper.insert(entity);
			}
		}
		if(runnerMatchInfoList!=null && runnerMatchInfoList.size()>0) {
			for(RunnerMatchInfo entity:runnerMatchInfoList) {
				//外键设置
				entity.setMatchid(runnerMatch.getId());
				runnerMatchInfoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		runnerMatchPlayerMapper.deleteByMainId(id);
		runnerMatchDeviceMapper.deleteByMainId(id);
		runnerMatchGunsMapper.deleteByMainId(id);
		runnerMatchInfoMapper.deleteByMainId(id);
		runnerMatchMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			runnerMatchPlayerMapper.deleteByMainId(id.toString());
			runnerMatchDeviceMapper.deleteByMainId(id.toString());
			runnerMatchGunsMapper.deleteByMainId(id.toString());
			runnerMatchInfoMapper.deleteByMainId(id.toString());
			runnerMatchMapper.deleteById(id);
		}
	}

	@Override
	public boolean updateMatchStatus(String id,String flag) {
		UpdateWrapper<RunnerMatch> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("mstatus",flag).eq("id",id);
		boolean exists = false;
		if("jxz".equals(flag)){
			UpdateWrapper<RunnerMatch> updateWrapperTemp = new UpdateWrapper<>();
			updateWrapperTemp.eq("mstatus",flag);
			exists =runnerMatchMapper.exists(updateWrapperTemp);
		}
		if(!exists){
			runnerMatchMapper.update(null,updateWrapper);
			RunnerMatch runnerMatch = runnerMatchMapper.selectById(id);
			Integer timeinterval = runnerMatch.getTimeinterval();
			if(null!=timeinterval&&timeinterval>10){
				MqttUtil.TIME_INTERVAL=timeinterval;
			}
			return true;
		}
		return false;
	}

}
